#!coding:utf8


# 这是一个很常见的统计序列中元素出现次数的代码

def get_counts(sequence):
    counts = {}
    for x in sequence:
        if x in counts:
            counts[x] += 1
        else:
            counts[x] = 1
    return counts

# 如果非常了解Python标准库, 那么可能会这样:

from collections import defaultdict

def get_counts2(sequence):
    counts = defaultdict(int)   # 所有的值均会被初始化为0

    for x in sequence:
        counts[x] += 1
    return counts


# Python标准库中还有collections.Counter类


from collections import Counter
counts = Counter(sequence)
print counts.most_common(10)  # 答应计数最多的前10项